extends PropertyFindSetter


class_name WeaponSetter
"""
设置武器相关属性
"""


## 更换武器类别
@export var weapon_type: int = 0
## 更换子弹类型
@export var scene: PackedScene = null
## 更换子弹设置
@export var source: ProjectileResource = null


# 缓存原来的数据
var w_scene: PackedScene = null
var w_source: ProjectileResource = null


func execute() -> void:
    if not IComponent.is_available(self):
        return
    var c: ICharacterComponent = get_wrapper()
    if not c:
        return
    var w: GunWeapon = c.get_character().subassembly(property)
    if not w:
        return
    if source != null:
        NodeHelper.set_property(w, "source", source, is_deferred)
        w_source = source
    if scene != null:
        if scene != w.factory.resource_scene:
            # 清除已发射的子弹
            w.wipe()
            NodeHelper.set_property(w.factory, "resource_scene", scene, is_deferred)
        w_scene = scene
    var weapon: Attribute = c.get_attribute("weapon")
    if weapon:
        var power: Attribute = c.get_attribute("power")
        # 武器相同时才提升等级
        if power and weapon.value == weapon_type:
            power.value += 1
        weapon.value = weapon_type
